#include "initTask.h"
#include <stdio.h>
#include "tx_api.h"
#include "drvClk.h"
#include "drvLog.h"
#include "drvLcd.h"
#include "drvSdram.h"
#include "guixTask.h"
#include "drvSD.h"
#include "util.h"
#include "filesystem.h"
#include "drvExtflash.h"

CALL_CHAIN_S PeriodCallHead = {NULL, NULL, NULL};

TX_THREAD InitTaskCtrlBlock;
static uint64_t InitTaskStack[1024];

TX_TIMER PeriodCheckTimer;
VOID periodCheckCallback(ULONG input);

void initTaskEntry(ULONG thread_input);

void tx_application_define(void *first_unused_memory)
{
    /* Create the main thread.  */
    tx_thread_create(&InitTaskCtrlBlock,
        "",
        initTaskEntry,
        0,
        InitTaskStack, 
        sizeof(InitTaskStack),
        TX_PRIORITIE_INIT,
        TX_PRIORITIE_INIT,
        TX_NO_TIME_SLICE,
        TX_AUTO_START);
}

void initTaskEntry(ULONG thread_input)
{
    //hardware init
    drvClkInit();
    drvQspiFlashInit();
    drvLogInit();
    drvSdramInit();
    drvLcdInit();
    //os resource init
    tx_timer_create(&PeriodCheckTimer, "", periodCheckCallback, 0, 100, 10, TX_AUTO_ACTIVATE);
    //tasks init
    guixTaskInit();
    filexTaskInit();
    while(1)
    {
        tx_thread_sleep(1000);
    }
}

VOID periodCheckCallback(ULONG input)
{
    CALL_CHAIN_S *temp = &PeriodCallHead;
    while(temp != NULL) {
        (temp->ptr)();
        temp = temp->next;
    }
}